package com.groupbuying.pushordermsg.util;

import cn.jiguang.common.ClientConfig;
import com.groupbuying.pushordermsg.JPushClient;
import com.groupbuying.pushordermsg.push.PushResult;
import com.groupbuying.pushordermsg.push.model.Options;
import com.groupbuying.pushordermsg.push.model.Platform;
import com.groupbuying.pushordermsg.push.model.PushPayload;
import com.groupbuying.pushordermsg.push.model.audience.Audience;
import com.groupbuying.pushordermsg.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @anthor wenguoqiang
 * @date: 2019/04/16 13:15
 * @company 够样
 * @des
 **/
@Component
public class PushUtils {

    protected static final Logger LOG = LoggerFactory.getLogger(PushUtils.class);

    @Value("${APP_KEY}")
    private String APP_KEY;
    @Value("${MASTER_SECRET}")
    private String MASTER_SECRET;


    //设置推送消息目标  设备别名 alias（店铺ID+收银台ID）  消息内容 notification （订单金额）

    public  PushPayload buildPushObject_all_alias_alert(String alias, String notification) {
        Map<String,String> map=new HashMap<String,String>();
        map.put(" mutable-content","1");

        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                //.setAudience(Audience.registrationId(REGISTRATION_ID3))
                .setAudience(Audience.alias(alias))
                //.setNotification(Notification.alert(notification))
                //.setNotification(Notification.ios(notification, map))
                .setNotification(Notification.iosandroid(notification,map))
                .setOptions(Options.newBuilder().setApnsProduction(true).setTimeToLive(86000).build())
                .build();
    }


    //极光推送服务

    public PushResult  JPushMsg(PushPayload payload) {
        //够样消息推送
        ClientConfig clientConfig = ClientConfig.getInstance();
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
        // PushPayload payload = buildPushObject_all_alias_alert();
        try {
            PushResult result = jpushClient.sendPush(payload);
            LOG.info("极光推送 Got result - " + result);
            LOG.info("推送信息 result - " + payload.toString());
            return result;
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
